﻿using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Furion.Core
{
    public  class Role: IEntity, IEntityTypeBuilder<Role> , IEntitySeedData<Role>
    {
        public Role()
        {
            CreatedTime = DateTime.Now;            
            Enabled = true;            
        }


        [Key]
        public int Id { get; set; }



        /// <summary>
        /// 角色名称
        /// </summary>
        [Required, MaxLength(50)]
        [Comment("角色名称")]
        public string Name { get; set; }

        /// <summary>
        /// 角色描述
        /// </summary>
        [MaxLength(100)]
        [Comment("角色描述")]
        public string Remark { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        [Comment("是否启用")]
        public bool Enabled { get; set; }



        /// <summary>
        /// 创建时间
        /// </summary>
        [Comment("创建时间")]
        public DateTime CreatedTime { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        [Comment("更新时间")]
        public DateTime UpdatedTime { get; set; }


        /// <summary>
        /// 创建者Id
        /// </summary>
        [Comment("创建者Id")]
        public long? CreatedUserId { get; set; }

        /// <summary>
        /// 创建者名称
        /// </summary>
        [Comment("创建者名称")]
        [MaxLength(20)]
        public string CreatedUserName { get; set; }

        /// <summary>
        /// 修改者Id
        /// </summary>
        [Comment("修改者Id")]
        public long? UpdatedUserId { get; set; }

        /// <summary>
        /// 修改者名称
        /// </summary>
        [Comment("修改者名称")]
        [MaxLength(20)]
        public string UpdatedUserName { get; set; }

        /// <summary>
        /// 软删除
        /// </summary>
        [JsonIgnore]
        [Comment("软删除标记")]
        public bool IsDeleted { get; set; } = false;


        /// <summary>
        /// 多对多
        /// </summary>
        public ICollection<User> Users { get; set; }

        /// <summary>
        /// 多对多中间表
        /// </summary>
        public List<UserRole> UserRoles { get; set; }

        /// <summary>
        /// 多对多
        /// </summary>
        public ICollection<Permission> Permissions { get; set; }

        /// <summary>
        /// 多对多中间表
        /// </summary>
        public List<RolePermission> RolePermissions { get; set; }







        /// <summary>
        /// 配置多对多关系
        /// </summary>
        /// <param name="entityBuilder"></param>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        public void Configure(EntityTypeBuilder<Role> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasMany(p => p.Permissions)
                .WithMany(p => p.Roles)
                .UsingEntity<RolePermission>(
                  u => u.HasOne(c => c.Permission).WithMany(c => c.RolePermissions).HasForeignKey(c => c.PermissionId)
                , u => u.HasOne(c => c.Role).WithMany(c => c.RolePermissions).HasForeignKey(c => c.RoleId)
                , u =>
                {
                    u.HasKey(c => new { c.RoleId, c.PermissionId });
                    // 添加多对多种子数据
                    u.HasData(
                        new { RoleId = 1, PermissionId = 1 },
                        new { RoleId = 1, PermissionId = 2 },
                        new { RoleId = 1, PermissionId = 3 },
                        new { RoleId = 1, PermissionId = 4 },
                        new { RoleId = 1, PermissionId = 5 },
                        new { RoleId = 1, PermissionId = 6 },
                        new { RoleId = 1, PermissionId = 7 },
                        new { RoleId = 1, PermissionId = 8 },
                        new { RoleId = 1, PermissionId = 9 },
                        new { RoleId = 1, PermissionId = 10 },
                        new { RoleId = 1, PermissionId = 11 },
                        new { RoleId = 1, PermissionId = 12 },
                        new { RoleId = 1, PermissionId = 13 },
                        new { RoleId = 1, PermissionId = 14 },
                        new { RoleId = 1, PermissionId = 15 },
                        new { RoleId = 1, PermissionId = 16 },
                        new { RoleId = 1, PermissionId = 17 },
                        new { RoleId = 1, PermissionId = 18 },
                        new { RoleId = 1, PermissionId = 19 },
                        new { RoleId = 1, PermissionId = 20 },
                        new { RoleId = 1, PermissionId = 21 },
                        new { RoleId = 1, PermissionId = 22 },
                        new { RoleId = 1, PermissionId = 23 },
                        new { RoleId = 1, PermissionId = 24 },
                        new { RoleId = 1, PermissionId = 25 },
                        new { RoleId = 1, PermissionId = 26 },
                        new { RoleId = 1, PermissionId = 27 },
                        new { RoleId = 1, PermissionId = 28 },
                        new { RoleId = 1, PermissionId = 29 },
                        new { RoleId = 1, PermissionId = 30 },
                        new { RoleId = 1, PermissionId = 31 },
                        new { RoleId = 1, PermissionId = 32 },



                        new { RoleId = 99, PermissionId = 1 },
                        new { RoleId = 99, PermissionId = 2 },
                        new { RoleId = 99, PermissionId = 3 },                        
                        new { RoleId = 99, PermissionId = 6 },
                        new { RoleId = 99, PermissionId = 7 },
                        new { RoleId = 99, PermissionId = 8 },                       
                        new { RoleId = 99, PermissionId = 22 },
                        new { RoleId = 99, PermissionId = 23 },
                        new { RoleId = 99, PermissionId = 24 },
                        new { RoleId = 99, PermissionId = 25 },
                        new { RoleId = 99, PermissionId = 26 },
                        new { RoleId = 99, PermissionId = 27 }
                         



                    );
                });
        }


        /// <summary>
        /// 种子数据
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        /// <returns></returns>
        public IEnumerable<Role> HasData(DbContext dbContext, Type dbContextLocator)
        {
            return new[]
            {
                new Role
                {
                    Id=1, Name="管理员",Remark="管理员"
                },
                new Role
                {
                    Id=99, Name="小程序用户",Remark="小程序用户"
                }
            };
        }


    }
}
